package com.shopping.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shopping.system.model.SysUser;
import com.shopping.system.service.SysUserService;
import com.shopping.common.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理 controller
 */
@RestController
@RequestMapping("/sysUser")
@Api(tags = "用户管理")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping
    @ApiOperation(value = "获取所有用户信息", notes = "获取所有用户信息")
    public Result getGoods(@ApiParam(name = "sysUser", value = "用户信息") SysUser sysUser) {
//        throw new RuntimeException("模拟异常");
        // 构建查询条件
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(sysUser.getName())) {
            queryWrapper.like("name", sysUser.getName());
        }
        if (!ObjectUtils.isEmpty(sysUser.getUsername())){
            queryWrapper.eq("username", sysUser.getUsername());
        }
        // 分页
//        PageHelper.startPage(sysUser.getPage(), sysUser.getSize());
        return Result.ok(sysUserService.list(queryWrapper));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取用户信息", notes = "根据id获取用户信息")
    public Result getGoodsById(@ApiParam(name = "id", value = "用户id", required = true) @PathVariable Integer id) {
        return Result.ok(sysUserService.getById(id));
    }


    @PostMapping
    @ApiOperation(value = "根据id修改用户信息", notes = "根据id修改用户信息")
    public Result updateGoodsById(@ApiParam(name = "sysUser", value = "用户信息", required = true)@RequestBody SysUser sysUser) {
        return Result.ok(sysUserService.updateById(sysUser));
    }


    @PutMapping
    @ApiOperation(value = "新增用户信息", notes = "新增用户信息")
    public Result addGoods(@ApiParam(name = "sysUser", value = "用户信息", required = true)@RequestBody SysUser sysUser) {
        return Result.ok(sysUserService.save(sysUser));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除用户信息", notes = "根据id删除用户信息")
    public Result deleteGoodsById(@ApiParam(name = "id", value = "用户id", required = true) @PathVariable Integer id) {
        return Result.ok(sysUserService.removeById(id));
    }

}
